package leetcode.editor.offer;

// https://leetcode.cn/problems/fan-zhuan-dan-ci-shun-xu-lcof/
class ZuoXuanZhuanZiFuChuanLcof {
    public static void main(String[] args) {
        Solution solution = new ZuoXuanZhuanZiFuChuanLcof().new Solution();
        solution.reverseLeftWords("abcdefg", 2);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        /*
         * 双指针
         *
         * @param s
         * @param n
         * @return
         *//*
        public String reverseLeftWords(String s, int n) {
            // 先将字符串局部反转
            s = reverse(s, 0, n - 1);
            s = reverse(s, n, s.length() - 1);
            // 再将字符串进行全局反转
            s = reverse(s, 0, s.length() - 1);
            return s;
        }


        */

        /**
         * 反转区间内的字符串
         *
         * @param s
         * @param i
         * @param j
         * @return
         *//*
        public String reverse(String s, int i, int j) {
            char[] chars = s.toCharArray();
            for (; i < j; i++, j--) {
                char temp = chars[i];
                chars[i] = chars[j];
                chars[j] = temp;
            }
            return String.valueOf(chars);
        }*/
        public String reverseLeftWords(String s, int n) {
            s = reverse(s, 0, n - 1);
            s = reverse(s, n, s.length() - 1);
            s = reverse(s, 0, s.length() - 1);
            return s;
        }

        public String reverse(String s, int left, int right) {
            char[] array = s.toCharArray();
            for (; left < right; left++, right--) {
                char temp = array[left];
                array[left] = array[right];
                array[right] = temp;
            }
            return String.valueOf(array);
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
